package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.result.ResultCode;
import com.atguigu.yygh.hosp.entity.HospitalSet;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-10-31
 */

@RestController
@Api ("医院设置接口")
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    HospitalSetService hospitalSetService;


    /**
     * 医院列表查询
     * @return
     */
    @ApiOperation ("医院列表查询")
    @GetMapping ("findAll")
    public ResultCode.R findAll() {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<> ();
        queryWrapper.eq ("is_deleted",0);
        List<HospitalSet> list = hospitalSetService.list(queryWrapper);
        return ResultCode.R.ok().data ("list",list);
    }

    /**
     * 医院设置删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "医院设置删除")
    @DeleteMapping ("{id}")
    public ResultCode.R removeById(
            @ApiParam (name = "id", value = "ID", required = true)
            @PathVariable String id){
        HospitalSet hospitalSet = hospitalSetService.getById (id);
        hospitalSet.setIsDeleted (1);
        boolean remove = hospitalSetService.updateById (hospitalSet);
        if (remove){
            return ResultCode.R.ok ();
        }else {
            return ResultCode.R.error ().message ("删除失败了");
        }
    }

    /**
     * 医院分页查询
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "分页医院设置列表")
    @GetMapping("{page}/{limit}")
    public ResultCode.R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<HospitalSet> pageParam = new Page<>(page, limit);


        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq ("is_deleted",0);
        hospitalSetService.page(pageParam, queryWrapper);
        List<HospitalSet> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return  ResultCode.R.ok().data("total", total).data("rows", records);
    }

    /**
     * 分页条件医院设置列表
     * @param page
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @ApiOperation(value = "分页条件医院设置列表")
    @PostMapping("{page}/{limit}")
    public ResultCode.R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "hospitalSetQueryVo", value = "查询对象", required = false)
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){

        Page<HospitalSet> pageParam = new Page<>(page, limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq ("is_deleted",0);
        if (hospitalSetQueryVo == null){
            hospitalSetService.page(pageParam, queryWrapper);
        } else {
            String hosname = hospitalSetQueryVo.getHosname();
            String hoscode = hospitalSetQueryVo.getHoscode();
            if (!StringUtils.isEmpty(hosname)) {
                queryWrapper.like("hosname", hosname);
            }
            if (!StringUtils.isEmpty(hoscode) ) {
                queryWrapper.eq("hoscode", hoscode);
            }
            hospitalSetService.page(pageParam, queryWrapper);
        }
        List<HospitalSet> records = pageParam.getRecords();
        for (HospitalSet record : records) {
            if (record.getStatus ()==0){
                record.setStatusBoolean (true);
            }else {
                record.setStatusBoolean (false);
            }
        }
        long total = pageParam.getTotal();
        return  ResultCode.R.ok().data("total", total).data("rows", records);
    }

    /**
     * 新增医院设置
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "新增医院设置")
    @PostMapping("saveHospSet")
    public ResultCode.R save(
            @ApiParam(name = "hospitalSet", value = "医院设置对象", required = true)
            @RequestBody HospitalSet hospitalSet){
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        //Random random = new Random();
        //hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        hospitalSetService.saveOrUpdate (hospitalSet);
        return ResultCode.R.ok();
    }

    /**
     * 根据ID查询医院设置
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID查询医院设置")
    @GetMapping("getHospSet/{id}")
    public ResultCode.R getById(
            @ApiParam(name = "id", value = "医院设置ID", required = true)
            @PathVariable String id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return ResultCode.R.ok().data("item", hospitalSet);
    }

    /**
     * 根据ID修改医院设置
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "根据ID修改医院设置")
    @PostMapping("updateHospSet")
    public ResultCode.R updateById(@ApiParam(name = "hospitalSet",value = "医院设置对象",required = true)
                        @RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return ResultCode.R.ok();
    }

   /* @ApiOperation(value = "根据ID修改医院设")
    @PutMapping("lockHospitalSet/${id}/${statusId}")
    public ResultCode.R lockHospitalSet(@PathVariable("id") Long id,@PathVariable("statusId")  Long statusId){
        hospitalSetService.lockHospitalSet(id,statusId);
        return ResultCode.R.ok();
    }*/

    /**
     * 批量删除医院设置
     * @param idList ：
     */
    @DeleteMapping("batchRemove")
    public ResultCode.R batchRemove(@RequestBody List<Long> idList) {
        hospitalSetService.removeHospSetByIds(idList);
        return ResultCode.R.ok();

    }

    /**
     *     医院设置锁定和解锁
     */
    @PutMapping("lockHospitalSet/{id}/{status}")
    public ResultCode.R lockHospitalSet(@PathVariable Long id, @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return ResultCode.R.ok();
    }

    public ResultCode.R saveHospList(){
        return ResultCode.R.ok();
    }

}

